package com.javasm.dao.impl;

import com.javasm.dao.EditAdminDao;
import com.javasm.entity.AdminUser;
import com.javasm.entity.sql.LoginManageSql;
import com.javasm.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/18 21:40
 * @since JDK 1.8
 */
public class EditAminDaoImpl implements EditAdminDao {
    /**
     * 通过id修改用户信息
     *
     * @param adminUser 要修改的用户对象
     * @return
     */
    @Override
    public int updateUserInfo(AdminUser adminUser) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(LoginManageSql.UPDATE_ADMIN_BY_ID, adminUser.getAname(), adminUser.getRole_id(), adminUser.getIsvalid(), adminUser.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 通过id删除用户
     *
     * @param aid 要删除的用户id
     * @return
     */
    @Override
    public int deleteUserInfo(String aid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(LoginManageSql.DELETE_ADMIN_BY_ID, aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 新增用户信息
     *
     * @param adminUser 要新增的用户对象
     * @return
     */
    @Override
    public int insertUserInfo(AdminUser adminUser) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(LoginManageSql.INSERT_ADMIN, adminUser.getAname(), adminUser.getApwd(), adminUser.getRole_id(), adminUser.getIsvalid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新用户权限信息
     *
     * @param aid          用户id
     * @param authorityArr 权限列表
     * @return
     */
    @Override
    public int updateUserAuthority(String aid, String authorityArr) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(LoginManageSql.UPDATE_ADMIN_MENU_BY_AID, authorityArr, aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
